/*
 * NetworkRequest.cpp
 *
 *  Created on: Nov 28, 2014
 *      Author: toti
 */

#include <Network/NetworkRequests/NetworkRequest.hpp>


NetworkRequest::NetworkRequest(NetworkRequestType networkRequestType){

	this->networkRequestType = networkRequestType;

	requestId = "";
	sender = "";
	voters = 0;
	localClient = 0;

	positiveResponses = 0;
	negativeResponses = 0;

	networkRequestState = UNDECIDED;

}

void NetworkRequest::addResponse(int response){

	if(response){
		positiveResponses++;
	}else{
		negativeResponses++;
	}

	switch(networkRequestType){

	case NICK_REQUEST:
	case CHANNEL_CREATE_REQUEST:

		/*
		 * Majority decision
		 */
		if((positiveResponses+negativeResponses) == voters){

			if(positiveResponses>negativeResponses){
				networkRequestState = ACCEPTED;
			}else{
				networkRequestState = REJECTED;
			}

		}else{
			networkRequestState = UNDECIDED;
		}

		break;

	case CHANNEL_REMOVE_REQUEST:

		/*
		 * Unanimously decision
		 */
		if((positiveResponses+negativeResponses) == voters){

			if(positiveResponses==voters){
				networkRequestState = ACCEPTED;
			}else{
				networkRequestState = REJECTED;
			}

		}else{
			networkRequestState = UNDECIDED;
		}

		break;

	}


}

void NetworkRequest::addResponse(std::string& response){
	if(!response.compare("true")){
		addResponse(1);
	}else{
		addResponse(0);
	}
}


void NetworkRequest::setLocalClient(LocalClient *localClient){
	this->localClient = localClient;
}

void NetworkRequest::setRequestId(std::string& requestId){
	this->requestId = requestId;
}

void NetworkRequest::setSender(std::string& sender){
	this->sender = sender;
}

void NetworkRequest::setVoters(int voters){
	this->voters = voters;
}


LocalClient *NetworkRequest::getLocalClient(){
	return localClient;
}

NetworkRequest::NetworkRequestState NetworkRequest::getRequestState(){
	return networkRequestState;
}

NetworkRequest::NetworkRequestType NetworkRequest::getNetworkRequestType(){
	return networkRequestType;
}

std::string& NetworkRequest::getRequestId(){
	return requestId;
}

std::string& NetworkRequest::getSender(){
	return sender;
}

int NetworkRequest::getVoters(){
	return voters;
}

